package io.adrainty.algorithm.leetcode.normal;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 1733. 需要教语言的最少人数
 * @link <a href="https://leetcode.cn/problems/minimum-number-of-people-to-teach/?envType=daily-question&envId=2025-09-09">每日一题</a>
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/9/10 下午10:36
 */
public class Solution1733 {

    public int minimumTeachings(int n, int[][] languages, int[][] friendships) {
        Set<Integer> noConn = new HashSet<>();
        for (int[] friendship : friendships) {
            Set<Integer> language1 = Arrays.stream(languages[friendship[0] - 1]).boxed().collect(Collectors.toSet());
            if (Arrays.stream(languages[friendship[1] - 1]).noneMatch(language1::contains)) {
                noConn.add(friendship[0]);
                noConn.add(friendship[1]);
            }
        }
        System.out.println(noConn);
        int[] languageCnt = new int[n];
        for (Integer i : noConn) {
            for (int language : languages[i - 1]) {
                languageCnt[language - 1]++;
            }
        }

        int maxCnt = languageCnt[0];
        for (int i = 1; i < n; i++) {
            if (languageCnt[i] > maxCnt) {
                maxCnt = languageCnt[i];
            }
        }

        return noConn.size() - maxCnt;
    }

    public static void main(String[] args) {
        Solution1733 solution1733 = new Solution1733();
        int ans = solution1733.minimumTeachings(
                3,
                new int[][]{new int[]{2}, new int[]{1,3}, new int[]{1, 2}, new int[]{3}},
                new int[][]{new int[]{1, 4}, new int[]{1, 2}, new int[]{2, 3}, new int[]{3, 4}}
        );
        System.out.println(ans);
    }

}
